// JavaScript Document
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
}


function v(id, nullable, type)
{
	if(nullable == 1)
	{
		//alert($('#'+id).val());
		
		
		if($('#'+id).val() != '')
		{
			if(type == 'email')
			{
				if(echeck($('#'+id).val()) == false)
				{
					$('#'+id).val('Must be Valid Email');
					vFlag = false;
				}
				else
				{
					//vFlag = true;
				}
			}
			else
			{
				//vFlag = true;
			}
		}
		else
		{
			$('#'+id).val('Must be Filled');
			vFlag = false;
		}
	}
}

function loadNotificationPage(area)
{
	//alert(area);
	$.post('inc/validation.php',{},function(data){
			$('#'+area).html(data);
			//alert(data);
	});
}

function loading()
{
	return '<img src="iconset/working.gif" />';
}

function toggleArea(areaid)
{
	$('#'+areaid).slideToggle('');
}

function readCookie(name)

{

  var cookieValue = "";

  var search = name + "=";

  if(document.cookie.length > 0)

  { 

    offset = document.cookie.indexOf(search);

    if (offset != -1)

    { 

      offset += search.length;

      end = document.cookie.indexOf(";", offset);

      if (end == -1) end = document.cookie.length;

      cookieValue = unescape(document.cookie.substring(offset, end))

    }

  }

  return cookieValue;

}



// ================= Form Submition and validation =========================
function doSubmit(formid, response_area, url)
{
	//alert(url);
	$('#'+response_area).html(loading());
	$('#'+formid).ajaxForm(function(data){
									$('#'+response_area).html('');
									//alert(data);
									if(data.trim() != 'success')
									{
										tellError(data);
										//loadNotificationPage('dialog');
										//$('#dialog').dialog('open');
									}
									else
									{
										//alert(data);
										//var url = '?page=notification&cmd=register-success';
										window.location = url;
										//alert(url);
									}
									});
}

function parseResponse(data)
{
	if(data == '')
	{
		return 'Ok'
	}
	else
	{
		arr = data.split(';');
		return arr;
	}
}

function tellError(data)
{
	$('span').remove();
	
	response = parseResponse(data);
	//alert(response);
	response.pop();		//removing the last NULL element of array
	$(response).each(function()
							{
							//alert(this);
							var i = this.split('|');
							var item_id = i[0].trim();
							var error_title = i[1].trim();
							var warning = '<br /><span id="alert-"'+item_id+'" style="color:red; font-size:11px">'+error_title+'</span>';
							//alert(item_id);
							$(warning).insertAfter("#"+item_id);
							$("#"+item_id).css('border-color', 'red');
							//$('#'+'_mallname').html(warning);
							$("#"+item_id).blur(function(){
													 $(this).css('border-color' , 'green');
													 //alert('#alert-'+this.id);
													 //$('#alert-'+this.id).html('Lets check again');
													 //
													});
							});
}

function search_me()
{
	var val = $('input[name$="keywords"]').val();
	//alert(val);
	if(val == '')
	{
		alert('Search cannot be empty string');
		return false
	}
	else
	{
		//alert(val);
		return true;
	}
}	
	
	